找传奇、传世资源到传世资源站!

C# 图片缩放示例源码(放大缩小)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboard
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace 图片缩放{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private double height = 0; private double width = 0; private void button1_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Title = "请选择一个图片文件"; dialog.Filter = "图片文件(*.jpg)|*.jpg"; if (dialog.ShowDialog() == DialogResult.OK) { string file = dialog.FileName; pictureBox1.Load(file); pictureBox2.Load(file); } } private void button2_Click(object sender, EventArgs e)//图片放大按钮事件 { button3.Enabled = true; pictureBox2.Height = Convert.ToInt32(pictureBox2.Height * 1.1); pictureBox2.Width = Convert.ToInt32(pictureBox2.Width * 1.1); panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum / 2 - panel1.VerticalScroll.LargeChange / 2; if (panel1.VerticalScroll.Value != panel1.VerticalScroll.Maximum / 2 - panel1.VerticalScroll.LargeChange / 2) panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum / 2 - panel1.VerticalScroll.LargeChange / 2; panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Maximum / 2 - panel1.HorizontalScroll.LargeChange / 2; if (panel1.HorizontalScroll.Value != panel1.HorizontalScroll.Maximum / 2 - panel1.HorizontalScroll.LargeChange / 2) panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Maximum / 2 - panel1.HorizontalScroll.LargeChange / 2; } private void button3_Click(object sender, EventArgs e)//图片缩小按钮事件 { if (pictureBox2.Height > height && pictureBox2.Width > width) { pictureBox2.Height = Convert.ToInt32(pictureBox2.Height * 0.9); pictureBox2.Width = Convert.ToInt32(pictureBox2.Width * 0.9); panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum / 2 - panel1.VerticalScroll.LargeChange / 2; if (panel1.VerticalScroll.Value != panel1.VerticalScroll.Maximum / 2 - panel1.VerticalScroll.LargeChange / 2) panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum / 2 - panel1.VerticalScroll.LargeChange / 2; panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Maximum / 2 - panel1.HorizontalScroll.LargeChange / 2; if (panel1.HorizontalScroll.Value != panel1.HorizontalScroll.Maximum / 2 - panel1.HorizontalScroll.LargeChange / 2) panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Maximum / 2 - panel1.HorizontalScroll.LargeChange / 2; } else { button3.Enabled = false; } } private void pictureBox1_MouseEnter(object sender, EventArgs e) { pictureBox1.Focus(); } private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) { if (e.Delta > 0) //放大图片 { pictureBox1.Size = new Size(pictureBox1.Width 50, pictureBox1.Height 50); } else { //缩小图片 pictureBox1.Size = new Size(pictureBox1.Width - 50, pictureBox1.Height - 50); } //设置图片在窗体居中 pictureBox1.Location = new Point((this.Width - pictureBox1.Width) / 2, (this.Height - pictureBox1.Height) / 2); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复